package com.ruoyi.cat.service.impl;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.cat.domain.AdGift;
import com.ruoyi.cat.domain.AdGiftImage;
import com.ruoyi.cat.domain.AdGiftLike;
import com.ruoyi.cat.mapper.AdGiftMapper;
import com.ruoyi.cat.service.IAdGiftImageService;
import com.ruoyi.cat.service.IAdGiftLikeService;
import com.ruoyi.cat.service.IAdGiftService;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 礼品单Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-05
 */
@Service
public class AdGiftServiceImpl implements IAdGiftService
{
    @Autowired
    private AdGiftMapper adGiftMapper;

    @Autowired
    private IAdGiftImageService iAdGiftImageService;

    @Autowired
    private IAdGiftLikeService iAdLikeRecordService;

    /**
     * 查询礼品单
     * 
     * @param id 礼品单主键
     * @return 礼品单
     */
    @Override
    public AdGift selectAdGiftById(Long id)
    {
        AdGift adGift = adGiftMapper.selectAdGiftById(id);
        AdGiftImage adGiftImage = new AdGiftImage();
        adGiftImage.setGiftId(id);
        adGift.setLikeSatate(adGift.getLikeSatate());
        adGift.setAdGiftImageList(iAdGiftImageService.selectAdGiftImageList(adGiftImage));
        return adGift;
    }

    @Override
    public AdGift selectAdGiftByUserId(Long id, Long userId) {
        AdGift adGift = adGiftMapper.selectAdGiftById(id);
        AdGiftImage adGiftImage = new AdGiftImage();
        adGiftImage.setGiftId(id);
        if (userId != null) {
            AdGiftLike adGiftLike = new AdGiftLike();
            adGiftLike.setUserId(userId);
            adGiftLike.setGiftId(id);
            List<AdGiftLike> list = iAdLikeRecordService.selectAdGiftLikeList(adGiftLike);
            adGift.setLikeSatate(list.size() > 0 ? "1" : "0");
        }
        adGift.setAdGiftImageList(iAdGiftImageService.selectAdGiftImageList(adGiftImage));
        return adGift;
    }

    /**
     * 查询礼品单列表
     * 
     * @param adGift 礼品单
     * @return 礼品单
     */
    @Override
    public List<AdGift> selectAdGiftList(AdGift adGift)
    {
        List<AdGift> list = adGiftMapper.selectAdGiftList(adGift);
        return list.stream().map(iAdGift->{
            AdGiftImage adGiftImage = new AdGiftImage();
            adGiftImage.setGiftId(iAdGift.getId());
            iAdGift.setAdGiftImageList(iAdGiftImageService.selectAdGiftImageList(adGiftImage));
            return iAdGift;
        }).collect(Collectors.toList());
    }

    /**
     * 新增礼品单
     * 
     * @param adGift 礼品单
     * @return 结果
     */
    @Override
    public int insertAdGift(AdGift adGift)
    {
        adGift.setCreateTime(DateUtils.getNowDate());
        int i = adGiftMapper.insertAdGift(adGift);
        // 增加非空判断，避免因 adGiftImageList 为 null 导致空指针异常
        List<AdGiftImage> adGiftImageList = adGift.getAdGiftImageList();
        if (adGiftImageList != null) {
            adGiftImageList.forEach(iAdGiftImage->{
                iAdGiftImage.setGiftId(adGift.getId());
                iAdGiftImage.setCreateTime(DateUtils.getNowDate());
                iAdGiftImageService.insertAdGiftImage(iAdGiftImage);
            });
        }
        return i;
    }

    /**
     * 修改礼品单
     * 
     * @param adGift 礼品单
     * @return 结果
     */
    @Override
    public int updateAdGift(AdGift adGift)
    {
       int i = adGiftMapper.updateAdGift(adGift);
        List<AdGiftImage> adGiftImageList = adGift.getAdGiftImageList();
        if (adGiftImageList != null) {
            iAdGiftImageService.deleteAdGiftImageById(adGift.getId());
            adGift.getAdGiftImageList().forEach(iAdGiftImage->{
                iAdGiftImage.setGiftId(adGift.getId());
                iAdGiftImage.setCreateTime(DateUtils.getNowDate());
                iAdGiftImageService.insertAdGiftImage(iAdGiftImage);
            });
        }
       return i;
    }

    @Override
    public int updateAdGiftBy(AdGift adGift) {
        return adGiftMapper.updateAdGift(adGift);
    }

    /**
     * 批量删除礼品单
     * 
     * @param ids 需要删除的礼品单主键
     * @return 结果
     */
    @Override
    public int deleteAdGiftByIds(Long[] ids)
    {
        return adGiftMapper.deleteAdGiftByIds(ids);
    }

    /**
     * 删除礼品单信息
     * 
     * @param id 礼品单主键
     * @return 结果
     */
    @Override
    public int deleteAdGiftById(Long id)
    {
        return adGiftMapper.deleteAdGiftById(id);
    }

    @Override
    public List<AdGift> getAdGiftList(Integer limit) {
        List<AdGift> list = adGiftMapper.getAdGiftList(limit);
        return list.stream().map(iAdGift->{
            AdGiftImage adGiftImage = new AdGiftImage();
            adGiftImage.setGiftId(iAdGift.getId());
            iAdGift.setAdGiftImageList(iAdGiftImageService.selectAdGiftImageList(adGiftImage));
            return iAdGift;
        }).collect(Collectors.toList());
    }

    @Override
    public List<AdGift> selectAdGiftListLimit(AdGift adGift) {
        List<AdGift> list = adGiftMapper.selectAdGiftListLimit(adGift);
        return list.stream().map(iAdGift->{
            AdGiftImage adGiftImage = new AdGiftImage();
            adGiftImage.setGiftId(iAdGift.getId());
            iAdGift.setAdGiftImageList(iAdGiftImageService.selectAdGiftImageList(adGiftImage));
            return iAdGift;
        }).collect(Collectors.toList());
    }
}
